import { action, flow, makeObservable, observable } from "mobx"

function mockSync (delay) {
  return new Promise(res => {
    setTimeout(res, delay)
  })
}

export default class AppleStore {

  constructor() {
    this.id = 1
    this.eatenNum = 0
    this.eatenWeight = 0
    this.appleList = []
    makeObservable(this, {
      id: observable,
      eatenNum: observable,
      eatenWeight: observable,
      appleList: observable,
      eat: action.bound,
      pick: flow.bound
    })
  }

  *pick() {
    yield mockSync(500)
    let weight = parseInt(200 + Math.random() * 100)
    this.appleList.push({
      id: this.id++,
      weight
    })
    console.log(this.appleList, 'applelist')
  }

  eat(id, weight) {
    const index = this.appleList.findIndex(item => item.id === id )
    this.appleList.splice(index, 1)
    this.eatenNum += 1
    this.eatenWeight += weight
  }
}